package com.example.windowmanager;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.rmondjone.camera.CameraActivity;
import com.rmondjone.camera.CameraPreview;
import com.rmondjone.camera.OverCameraView;
import com.rmondjone.camera.PermissionUtils;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;

public class MainActivity2 extends AppCompatActivity {
    /**
     * 相机类
     */
    private Camera mCamera;
    private FrameLayout frameLayout;
    /**
     * 聚焦视图
     */
    private OverCameraView mOverCameraView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        frameLayout = findViewById(R.id.frameLayout);


    }

    @Override
    protected void onResume() {
        super.onResume();
        mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        CameraPreview preview = new CameraPreview(this, mCamera);
        mOverCameraView = new OverCameraView(this);
        frameLayout.addView(preview);
        frameLayout.addView(mOverCameraView);
    }
    /**
     * 启动拍照界面
     *
     * @param activity
     * @param requestCode
     * @param type
     */
    public static void startMe(Activity activity, int requestCode, CameraActivity.MongolianLayerType type) {
        PermissionUtils.applicationPermissions(activity, new PermissionUtils.PermissionListener() {
            @Override
            public void onSuccess(Context context) {
                Intent intent = new Intent(activity, CameraActivity.class);
                intent.putExtra("MongolianLayerType", type);
                activity.startActivityForResult(intent, requestCode);
            }

            @Override
            public void onFailed(Context context) {
                if (AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.CAMERA)
                        && AndPermission.hasAlwaysDeniedPermission(context, Permission.Group.STORAGE)) {
                    AndPermission.with(context).runtime().setting().start();
                }
                Toast.makeText(context, context.getString(com.rmondjone.camera.R.string.permission_camra_storage), Toast.LENGTH_SHORT);
            }
        }, Permission.Group.STORAGE, Permission.Group.CAMERA);
    }

}